package com.example.security.funtion;

import com.example.core.utils.UserUtils;
import com.example.sys.auth.constant.AuthConstant;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class EditorFunction extends SysAbstractFunction {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public AviatorObject call(Map<String, Object> env) {
        if (env instanceof FunctionContext) {
            FunctionContext context = (FunctionContext) env;
            HttpServletRequest httpServletRequest = context.getHttpServletRequest();
            String id = httpServletRequest.getParameter(ID);
            if (!StringUtils.isEmpty(id)) {
                List<String> userAuthIdList = getSysElementService().getUserAuthIdList(UserUtils.getUserId());
                if (!getSysAuthService().exist(AuthConstant.EDITOR, context.getBaseService().getPoClass().getName(), id, userAuthIdList)) {
                    return AviatorBoolean.FALSE;
                }
            }
        }
        return AviatorBoolean.TRUE;
    }
}
